package com.lyy.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyy.Vo.FileVo;
import com.lyy.aspect.ControllerEndpoint;
import com.lyy.response.Result;
import com.lyy.system.entity.ImageInfo;
import com.lyy.system.entity.OssEntity;
import com.lyy.system.service.AliOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * OSS上传文件、删除文件
 */
@Api(value = "OSS上传",tags = "OSS接口")
@RestController
@CrossOrigin
public class AliOssController {

    @Autowired
    private AliOssService aliOssService;

    @ControllerEndpoint(exceptionMessage = "上传图片失败", operation = "上传图片")
    @PostMapping("/uploadImgFile")
    @ApiOperation(value = "上传图片文件")
    public Result uploadImgFile(MultipartFile file){
        String s=aliOssService.upload(file);
        return Result.ok().data("url",s);
    }

    @ControllerEndpoint(exceptionMessage = "删除图片失败", operation = "删除图片")
    @PostMapping("/deleteImgFile")
    @ApiOperation(value = "删除上传的头像文件")
    public Result deleteImgFile(String file){
        //file=http://xinguan-parent-suzhou.oss-cn-hangzhou.aliyuncs.com/2022/05/30/26d259f351b045b68d75b083e625f203.jpg
        try {
            String[] spilt=file.split(".com/");
            aliOssService.deleteFile(spilt[1]);
            return Result.ok();
        }catch (Exception e){
            //需要打印错误日志到本地系统中(服务器系统)
            return Result.error();
        }
    }

    @PostMapping("/queryListPage")
    @ApiOperation(value = "条件查询文件列表")
    public Result queryListPage(@RequestParam(required = true,defaultValue = "1") Integer current,
                                @RequestParam(required = true,defaultValue = "6") Integer size,
                                @RequestBody FileVo fileVo){
        Page<ImageInfo> page =new Page<>(current,size);
        QueryWrapper<ImageInfo> wrapper =new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        if (fileVo!=null){
            if (!StringUtils.isEmpty(fileVo.getFileName())){
                wrapper.like("name",fileVo.getFileName());
            }
            if (!StringUtils.isEmpty(fileVo.getMediaType())){
                wrapper.eq("media_type",fileVo.getMediaType());
            }
            if (!StringUtils.isEmpty(fileVo.getPath())){
                wrapper.eq("path",fileVo.getPath());
            }
            if (!StringUtils.isEmpty(fileVo.getSuffix())){
                wrapper.eq("suffix",fileVo.getSuffix());
            }
            if (!StringUtils.isEmpty(fileVo.getStartDate()) && !StringUtils.isEmpty(fileVo.getEndDate())){
                wrapper.between("SUBSTR(create_time,1,10)",fileVo.getStartDate(),fileVo.getEndDate());
            }
        }
        IPage<ImageInfo> fileListPage =aliOssService.queryListPage(page,wrapper);
        Long total =fileListPage.getTotal();
        List<ImageInfo> records =fileListPage.getRecords();
        return Result.ok().data("total",total).data("fileList",records);
    }

}
